package spring.cloud.service.task.utils;


import java.util.ArrayList;
import java.util.List;

public class PageInfo<T> {
    /**
     * 总记录数
     */
    private final int totalCount;
    /**
     * pageNum 第几页
     */
    private int pageNum;
    /**
     * pageSize 每页大小
     */
    private int pageSize;
    /**
     * pageTotal 总页数
     */
    private int totalPageNum;
    /**
     * currentPage 当前页
     */
    private int currentPageNum;
    /**
     * 起始索引
     */
    private int currentPageOffset;
    //
    private List<T> records = new ArrayList<>();

    /**
     * 没有数据的时候数据封装
     *
     * @param records
     */
    public PageInfo(List<T> records) {
        this.totalCount = 0;
        this.records = records;
    }


    /**
     * 有数据的时候数据封装
     *
     * @param pageNum
     * @param pageSize
     * @param totalCount
     */
    public PageInfo(int pageNum, int pageSize, int totalCount) {
        this.pageNum = pageNum;
        this.pageSize = pageSize;
        this.totalCount = totalCount;//必须有一页数据
        if (pageNum <= 0) {
            throw new IllegalArgumentException("pageNum必须大于等于1");
        }
        if (pageSize <= 0) {
            throw new IllegalArgumentException("pageSize必须大于等于1");
        }
        if (totalCount <= 0) {
            throw new IllegalArgumentException("不支持没有记录的分页计算");
        }
        //
        this.totalPageNum = totalCount % pageSize == 0 ? totalCount / pageSize : (totalCount / pageSize) + 1;
        if (this.pageNum > this.totalPageNum) {
            this.currentPageNum = this.totalPageNum;
        } else {
            this.currentPageNum = this.pageNum;
        }
        this.currentPageOffset = (this.currentPageNum - 1) * this.pageSize;
    }

    public int getCurrentPageOffset() {
        return currentPageOffset;
    }

    public int getTotalCount() {
        return totalCount;
    }

    public List<T> getRecords() {
        return records;
    }

    public void setRecords(List<T> records) {
        this.records = records;
    }
}